package com.authority.framework.web.controller;

import com.authority.framework.log.Log;
import com.authority.framework.log.enums.BusinessType;
import com.authority.framework.web.domain.SysPostEntity;
import com.authority.framework.web.domain.SysUserEntity;
import com.authority.framework.web.service.SysPostService;
import com.authority.framework.base.util.DateUtils;
import com.authority.framework.base.util.ShiroUtils;
import com.authority.framework.base.util.laydata.LayDatas;
import com.authority.framework.base.util.laydata.PageUtils;
import com.authority.framework.base.util.laydata.ResultModel;
import com.authority.framework.base.util.laydata.ResultTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: authority
 * @description: 岗位控制层
 * @author: Mr.Yan
 * @create: 2018-12-06 15:23
 **/
@Controller
@RequestMapping(value = "syspostCon")
public class SysPostController {

	private static final Logger log = LoggerFactory.getLogger(SysPostController.class);

	@Autowired
	private SysPostService sysPostService;

	private Map<String, Object> paramMap;//参数

	private Map<String, Object> resultMap;//返回值

	/**
	 * @Description: 获取岗位所有数据集
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/6
	 */
	@RequestMapping(value = "/findAll")
	@ResponseBody
	public ResultModel list(HttpServletResponse response, HttpServletRequest request) {
		List<SysPostEntity> posts = null;
		resultMap = new HashMap<>();
		try {
			posts = sysPostService.findAll(SysPostEntity.class);
			resultMap.put("posts", posts);
			return ResultTools.result(0, "", resultMap);
		}
		catch (Exception e) {
			return ResultTools.result(404, e.getMessage(), resultMap);
		}
	}

	/**
	 * @Description: 获取岗位数据级展示数据
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/15
	 */

	@RequestMapping("/list")
	@ResponseBody
	public LayDatas list(HttpServletRequest request, HttpServletResponse response,
			PageUtils pageUtils) {
		return sysPostService.pageLayInfo("system/SqlPostSql", SysPostEntity.class, pageUtils);
	}

	/**
	 * @Description: 岗位添加
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/24
	 */

	@Log(title = "岗位管理", businessType = BusinessType.INSERT)
	@RequestMapping("/insert")
	@ResponseBody
	public ResultModel insert(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> ajaxMap) {
		try {
			//  获取登陆用户信息
			SysUserEntity userEntity = ShiroUtils.getSubjectUser();
			ajaxMap.put("createBy", userEntity.getUserName());
			//  获取当前时间
			String time = DateUtils.getTime();
			ajaxMap.put("createTime", time);
			Integer insert = sysPostService.insert(SysPostEntity.class, ajaxMap);
			if (insert > 0) {
				return ResultTools.result(0, "", null);
			} else {
				return ResultTools.result(404, "添加岗位信息失败", null);
			}
		}
		catch (Exception e) {
			log.error("添加岗位信息出现错误" + e.getMessage());
			return ResultTools.result(404, "添加岗位信息出现错误", null);
		}
	}

	/**
	 * @Description: 打开修改岗位之前查询列表回显
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/24
	 */
	@ResponseBody
	@RequestMapping(value = "/openPostUpdate")
	public ModelAndView openMenuUpdate(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name = "postId", required = false) Integer postId) {
		ModelAndView mv = new ModelAndView();
		try {
			SysPostEntity postEntity = null;
			postEntity = sysPostService.findByPrimaryKey(SysPostEntity.class, postId);
			mv.addObject("post", postEntity);
		}
		catch (Exception e) {
			log.error("打开岗位修改框出现错误" + e.getMessage());
			e.printStackTrace();
		}
		mv.setViewName("web/system/post/post_update");
		return mv;
	}

	/**
	 * @Description: 更新部门信息
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/7
	 */
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	@ResponseBody
	@RequestMapping(value = "/update")
	public ResultModel update(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> ajaxMap) {
		try {
			//  获取当前时间
			String time = DateUtils.getTime();
			//  获取登陆用户信息
			SysUserEntity userEntity = ShiroUtils.getSubjectUser();
			ajaxMap.put("updateTime", time);
			ajaxMap.put("updateBy", userEntity.getUserName());
			Integer update = sysPostService.update(SysPostEntity.class, ajaxMap);
			if (update > 0) {
				return ResultTools.result(0, "", null);
			} else {
				return ResultTools.result(404, "更新岗位信息失败", null);
			}
		}
		catch (Exception e) {
			log.error("更新岗位信息出现错误" + e.getMessage());
			return ResultTools.result(404, "更新岗位信息出现错误", null);
		}
	}

	/**
	 * 删除 岗位信息
	 *
	 * @param request
	 * @param response
	 * @param postIds
	 * @return
	 */
	@Log(title = "岗位管理", businessType = BusinessType.DELETE)
	@ResponseBody
	@RequestMapping(value = "/deletePost")
	public ResultModel deleteDept(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name = "postIds", required = false) String postIds) {
		try {
			String[] postIdArr = postIds.split(",");
			for (String postId : postIdArr) {
				paramMap = new HashMap<>();
				paramMap.put("postId", postId);
				paramMap.put("delFlag", 2);
				//  获取当前时间
				String time = DateUtils.getTime();
				//  获取登陆用户信息
				SysUserEntity userEntity = ShiroUtils.getSubjectUser();
				paramMap.put("updateTime", time);
				paramMap.put("updateBy", userEntity.getUserName());
				Integer update = sysPostService.update(SysPostEntity.class, paramMap);
				if (update < 1) {
					return ResultTools.result(404, "删除岗位信息失败", null);
				}
			}
			return ResultTools.result(0, "", null);
		}
		catch (Exception e) {
			log.error("删除岗位信息出现未知错误" + e.getMessage());
			return ResultTools.result(404, "删除岗位信息出现未知错误", null);
		}
	}
}
